import { Component, OnInit, Input } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { MdlSnackbarService, MdlDialogService } from '@angular-mdl/core';
import { DynamicDialogRef, DynamicDialogConfig } from 'primeng/api';
import { ReportConfigService } from '../report-config.service'
import { SelectItem } from 'primeng/api';

@Component({
    templateUrl: './report-config-create.component.html',
    styleUrls: ['./report-config-create.component.scss'],
    providers: []
})
export class ReportConfigCreateComponent implements OnInit {
    report: any = {};
    reportCreateForm: FormGroup;
    //报表类型
    queryTypes: SelectItem[] = [{ label: '查询报表', value: 1 }, { label: '统计报表', value: 2 }, { label: '自定义报表', value: 3 }]
    currentReportTypeId = 1;
    //业务类型
    bizs: SelectItem[] = [];
    //数据源
    dataSources: SelectItem[] = [];
    //数据视图
    dataviews: SelectItem[] = [];

    constructor(
        private fb: FormBuilder,
        public ref: DynamicDialogRef,
        private reportConfigService: ReportConfigService,
        private mdlDialogService: MdlDialogService,
        private mdlSnackbarService: MdlSnackbarService) {
    }
    ngOnInit() {
        this.reportConfigService.loadSelectItems().subscribe(data => {
            let bizs = [];
            data.bizs.forEach(biz => {
                bizs.push({ label: biz.bizName, value: biz.bizId });
            });
            this.bizs = bizs;
            this.dataSources = data.ds;
            this.dsChange(null);
        })
        this.buildForm();
    }

    create() {
        let report = this.reportCreateForm.value;
        if (null == report.bizId) {
            report.bizId = this.bizs[0].value;
            report.bizName = this.bizs[0].label;
        }
        if(null == report.bizName){
            this.bizs.forEach(e=>{
                if(e.value = report.bizId){
                    report.bizName = e.label;
                    return;
                }
            });
        }
        if (null == report.dsId) {
            report.dsId = this.dataSources[0].value;
            report.dsName = this.dataSources[0].label;
        }
        if (null == report.reportTypeId) {
            report.reportTypeId = this.queryTypes[0].value;
            report.reportTypeName = this.queryTypes[0].label;
        }
        if(null == report.dataviewName){
            report.dataviewName = this.dataviews[0].label;
        }
        console.log(report);
        this.reportConfigService.createReport(report).subscribe(data => {
            this.ref.close(data);
            this.mdlSnackbarService.showToast('报表创建成功');
        });
    }

    buildForm(): void {
        this.reportCreateForm = this.fb.group({
            'reportId': [this.report.reportId],
            'reportName': [this.report.reportName],
            'groupName': [this.report.groupName],
            'dsId': [this.report.dsId],
            'bizId': [this.report.bizId],
            'bizName': [this.report.bizName],
            'reportTypeId': [this.report.reportTypeId],
            'reportUrl': [this.report.reportUrl],
            'dataviewName': [this.report.dataviewName],
            'displayOrder': [this.report.displayOrder]
        });
    }

    /**数据源切换 */
    dsChange(event) {
        let dsId = this.dataSources[0].value;
        if (event) dsId = event.value;
        this.reportConfigService.loadDataviews(dsId).subscribe(data => {
            let views = [];
            data.forEach(view => {
                views.push({ label: view, value: view });
            });
            this.dataviews = views;
        });
    }

    /**取消关闭窗口 */
    cancel() {
        this.ref.close();
    }

    reportTypeChange(event){
        this.currentReportTypeId = event.value;
    }
}
